package com.linzelin.www.po;

import com.linzelin.www.util.JDBCUtils;
import com.linzelin.www.util.Student;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.io.*;
import java.util.List;
import java.util.Scanner;


public class Account_student implements Serializable{
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    //先获取一波连接池
    public boolean login(String username,String password) {
        if (username == null || password == null) {
            return false;
        }
        String sql = "select * from student";
        //获取表中符合条件的账号,有就返回true,没有就返回false
        List<Student> list = template.query(sql,new BeanPropertyRowMapper<Student>(Student.class));
        for (Student student : list) {
            if(student.getUsename().equals(username))
            {
                while(!(student.getPassword().equals(password)))
                {
                    System.out.println("密码有误,重新输入");
                    password = new Scanner(System.in).next();
                }
                return true;
            }
        }
        return false;
    }



    /*Properties prop = new Properties();
    //创建一个用来存放的*/
    public void User_add(Student student) throws InterruptedException, IOException {
        if(!login(student.getUsename(),student.getPassword()))
        {
            String sql ="insert into student values (null,?,?,?)";
            template.update(sql,student.getUsename(),student.getPassword(),0);
            student.setDuration(0);
            System.out.println("正在注册\n=================");
            Thread.sleep(2000);
            System.out.println("注册成功");
        }
        else
        {
            String sql = "select * from student";
            //获取表中符合条件的账号,有就返回true,没有就返回false
            List<Student> list = template.query(sql,new BeanPropertyRowMapper<Student>(Student.class));
            for (Student s : list) {
                if(s.getUsename().equals(student.getUsename()))
                {
                    student.setDuration(s.getDuration());
                }
            }
            System.out.println("登录成功");

        }
        Operation(student);
    }
    /*
    public boolean User_find(Student student) throws IOException, InterruptedException {
        Properties prop_find = new Properties();
        prop_find.load(new FileReader("C:\\Users\\啦啦啦\\Desktop\\Student.txt"));
        Set<String> set = prop_find.stringPropertyNames();
        for (String s : set) {
            if(s.equals(student.getName()))
            {
                if(student.getCipher().equals(prop_find.get(s)))
                {
                    System.out.println("正在登入\n=================");
                    Thread.sleep(2000);
                    return false;
                }
                else {
                    String code;
                    while (!(prop_find.get(s).equals(code = new Scanner(System.in).next()))){
                        System.out.println("密码错误，请重新输入密码");
                    }
                    student.setCipher(code);
                    System.out.println("正在登入\n=================");
                    Thread.sleep(2000);
                    return false;
                }
            }
        }
        return true;
    }*/
    public void Operation(Student student) throws InterruptedException {
        boolean flag = true;
        while(flag)
        {
            System.out.println("请选择服务:\n1.参与活动\n2.查询时长\n3.退出");
            int select = new Scanner(System.in).nextInt();
            switch(select)
            {
                case 1:break;
                case 2:
                    System.out.println("您现在已有的时长有:"+student.getDuration());
                    break;
                case 3:Thread.sleep(1000);flag = false;break;
                default:
                    System.out.println("输入有误，请重新选择");
                    Operation(student);break;
            }
        }
    }

}
